package com.ruoyi.forum.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.forum.domain.ForumStudent;
import com.ruoyi.forum.domain.vo.ForumReviewReply;
import com.ruoyi.forum.domain.vo.ForumReviewUser;
import com.ruoyi.forum.domain.vo.ForumReviewVo;
import com.ruoyi.forum.mapper.ForumReviewMapper;
import com.ruoyi.forum.mapper.ForumSchoolMapper;
import com.ruoyi.forum.mapper.ForumStudentMapper;
import com.ruoyi.forum.service.IForumSchoolService;
import com.ruoyi.forum.service.IForumStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;
import com.ruoyi.forum.domain.ForumReview;
import com.ruoyi.forum.service.IForumReviewService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 论坛-评论Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-12
 */
@Service
public class ForumReviewServiceImpl implements IForumReviewService 
{
    @Autowired
    private ForumReviewMapper forumReviewMapper;

    @Autowired
    private IForumStudentService forumStudentService;

    @Autowired
    private IForumSchoolService forumSchoolService;

    /**
     * 查询论坛-评论
     * 
     * @param id 论坛-评论主键
     * @return 论坛-评论
     */
    @Override
    public ForumReview selectForumReviewById(Long id)
    {
        return forumReviewMapper.selectForumReviewById(id);
    }

    /**
     * 查询论坛-评论列表
     * 
     * @param forumReview 论坛-评论
     * @return 论坛-评论
     */
    @Override
    public List<ForumReview> selectForumReviewList(ForumReview forumReview)
    {
        return forumReviewMapper.selectForumReviewList(forumReview);
    }

    @Override
//    @Transactional
    public List<ForumReviewVo> selectForumReviewListForPostId(Long postId) {
        //根据postId获取到所有的评论
        ForumReview forumReview = new ForumReview();
        forumReview.setPostId(postId);
        //查询未删除的评论
        forumReview.setDelFlag("0");
        List<ForumReview> forumReviews = forumReviewMapper.selectForumReviewList(forumReview);
        //开始规格化评论
        List<ForumReviewVo> forumReviewVos = new ArrayList<ForumReviewVo>();
        HashMap<Long, ForumReviewVo> longForumReviewVoHashMap = new HashMap<>();
        List<ForumReviewVo> tempForumReviewVos = new ArrayList<ForumReviewVo>();
        for(ForumReview review:forumReviews){
            ForumReviewVo forumReviewVo = new ForumReviewVo();
            forumReviewVo.setId(review.getId());
            forumReviewVo.setUid(review.getUserId());
            ForumStudent forumStudent = forumStudentService.selectForumStudentById(review.getUserId());
            forumReviewVo.setAddress("来自《"+forumSchoolService.selectForumSchoolById(forumStudent.getSchoolId()).getName()+"》");
            forumReviewVo.setLikes(review.getUpvoteNumber());
            forumReviewVo.setContentImg(review.getContentImg());
            forumReviewVo.setContent(review.getContent());
            forumReviewVo.setCreateTime(review.getCreateTime());
            ForumReviewUser forumReviewUser = new ForumReviewUser();
            forumReviewUser.setUsername(forumStudent.getNickName()!=null?forumStudent.getNickName():forumReviewUser.getUsername());
            forumReviewUser.setAvatar("/prod-api"+forumStudent.getAvatar());
            forumReviewVo.setUser(forumReviewUser);
            if(review.getParentId()!=0l){
                forumReviewVo.setParentId(review.getParentId());
                tempForumReviewVos.add(forumReviewVo);
            }else {
                forumReviewVos.add(forumReviewVo);
                longForumReviewVoHashMap.put(forumReviewVo.getId(),forumReviewVo);
            }
        }
        for(ForumReviewVo forumReviewVo : tempForumReviewVos){
            ForumReviewVo review = longForumReviewVoHashMap.get(forumReviewVo.getParentId());
            if(review.getReply()!=null){
                ForumReviewReply reply = review.getReply();
                reply.setTotal(reply.getTotal()+1);
                List<ForumReviewVo> list = reply.getList();
                list.add(forumReviewVo);
            }else {
                ArrayList<ForumReviewVo> list = new ArrayList<>();
                list.add(forumReviewVo);
                ForumReviewReply forumReviewReply = new ForumReviewReply(1,list);
                review.setReply(forumReviewReply);
            }
        }
        return forumReviewVos;
    }

    /**
     * 查询论坛-某一天评论数量
     *
     * @param date 查询的时间。
     * @return 结果
     */
    @Override
    public int selectForumReviewByDate(Date date) {
        return forumReviewMapper.selectForumReviewByDate(date);
    }

    /**
     * 新增论坛-评论
     * 
     * @param forumReview 论坛-评论
     * @return 结果
     */
    @Override
    public int insertForumReview(ForumReview forumReview)
    {
        forumReview.setCreateTime(DateUtils.getNowDate());
        return forumReviewMapper.insertForumReview(forumReview);
    }

    /**
     * 修改论坛-评论
     * 
     * @param forumReview 论坛-评论
     * @return 结果
     */
    @Override
    public int updateForumReview(ForumReview forumReview)
    {
        forumReview.setUpdateTime(DateUtils.getNowDate());
        return forumReviewMapper.updateForumReview(forumReview);
    }

    /**
     * 批量删除论坛-评论
     * 
     * @param ids 需要删除的论坛-评论主键
     * @return 结果
     */
    @Override
    public int deleteForumReviewByIds(Long[] ids)
    {
        return forumReviewMapper.deleteForumReviewByIds(ids);
    }

    /**
     * 删除论坛-评论信息
     * 
     * @param id 论坛-评论主键
     * @return 结果
     */
    @Override
    public int deleteForumReviewById(Long id)
    {
        return forumReviewMapper.deleteForumReviewById(id);
    }
}
